@echo off
:: Script to download and install Visual Studio Code (VSCode) on Windows with manual installation options

:: Check if running as administrator
net session >nul 2>&1
if %errorLevel% == 0 (
    echo Running as administrator...
) else (
    echo Please run this script as administrator!
    pause
    exit /b
)

:: Set the correct download URL for VSCode Windows version
set VSCODE_URL=https://update.code.visualstudio.com/latest/win32-x64-user/stable

:: Set the default VSCode installation directory (default is C:\Users\<Username>\AppData\Local\Programs\Microsoft VS Code)
set INSTALL_DIR=%LOCALAPPDATA%\Programs\Microsoft VS Code

:: Check if VSCode is already installed by verifying the existence of the installation directory
if exist "%INSTALL_DIR%" (
    echo VSCode is already installed at %INSTALL_DIR%. Skipping download and installation.
    pause
    exit /b
)

:: Set the path to save the installer (Windows 11 default download directory)
set DOWNLOAD_PATH=%USERPROFILE%\Downloads\VSCode-Installer.exe

:: Download the VSCode installer using PowerShell
echo VSCode is not installed. Downloading VSCode installer...
powershell -Command "Invoke-WebRequest -Uri '%VSCODE_URL%' -OutFile '%DOWNLOAD_PATH%'"

:: Check if the download was successful
if not exist "%DOWNLOAD_PATH%" (
    echo Failed to download VSCode installer!
    pause
    exit /b
)

:: Install VSCode with manual options
echo Installing VSCode...
echo Please follow the installation wizard to complete the installation.
start /wait "" "%DOWNLOAD_PATH%"

:: Check if the installation was successful
if not exist "%INSTALL_DIR%" (
    echo VSCode installation failed!
    pause
    exit /b
)

:: Optionally, add VSCode to the system environment variables (if needed)
echo Adding VSCode to system environment variables...
set VSCODE_BIN_PATH=%INSTALL_DIR%\bin
setx PATH "%PATH%;%VSCODE_BIN_PATH%" /M

:: Clean up the installer
echo Cleaning up the installer...
del "%DOWNLOAD_PATH%"

echo VSCode installation completed successfully!
pause